package com.travel.recommend.service.impl;

import com.travel.common.to.AttractionInfoTo;
import com.travel.common.to.UpdateUserTagWeightTo;
import com.travel.recommend.entity.UserAttractionWeightEntity;
import com.travel.recommend.entity.UserBehaviorWeightEntity;
import com.travel.recommend.service.UserBehaviorWeightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.travel.common.utils.PageUtils;
import com.travel.common.utils.Query;

import com.travel.recommend.dao.UserTagWeightDao;
import com.travel.recommend.entity.UserTagWeightEntity;
import com.travel.recommend.service.UserTagWeightService;


@Service("userTagWeightService")
public class UserTagWeightServiceImpl extends ServiceImpl<UserTagWeightDao, UserTagWeightEntity> implements UserTagWeightService {

    @Autowired
    UserBehaviorWeightService userBehaviorWeightService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserTagWeightEntity> page = this.page(
                new Query<UserTagWeightEntity>().getPage(params),
                new QueryWrapper<UserTagWeightEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void updateTagWeight(Integer userId, String tagName, Double weight) {
        this.baseMapper.updateTagWeight(userId,tagName,weight);
    }

    /**
     * 更新用户对景点标签的权重
     */
    @Override
    public void updateUserTagWeight(UpdateUserTagWeightTo to, Integer userId) {
        // 判断用户对该类景点标签有无权重
        String tagName = to.getTagName();

        UserTagWeightEntity userTagWeightEntity = this.baseMapper.selectOne(new QueryWrapper<UserTagWeightEntity>()
                .eq("user_id", userId).eq("tag_name", tagName));

        if(userTagWeightEntity == null){
            // 无权重 新增
            UserTagWeightEntity entity = new UserTagWeightEntity();
            entity.setTagName(tagName);
            entity.setTagWeight(to.getWeight());
            entity.setUserId(userId);

            this.baseMapper.insert(entity);
        }else{
            // 有权重 累加/累减
            Double tagWeight = userTagWeightEntity.getTagWeight() + to.getWeight();
            userTagWeightEntity.setTagWeight(tagWeight);

            this.baseMapper.updateById(userTagWeightEntity);
        }

    }

}